from PySide6.QtGui import QColor

from common import npc as npc_module, quest as module_quest
from common.build import Portal, LinkPortal
from common.npc import Npc
from common.quest import QuestCategory, QuestStatus
from common.resources import Resource
from common.world import LatticeMap
from gui.reference import Pos
from gui.windows.tree_map import TreeNode
from utils.db import DBSession
from utils.reference import iter_dir, read_yaml
from utils.settings import ASSETS_DIR


def load_assets():
    # 加载所有npc
    for key, meta in read_yaml('npc.yaml'):
        DBSession.npc_map[key] = getattr(npc_module, key)(key, **meta)

    for p in iter_dir(ASSETS_DIR / 'equipments'):
        for key, meta in read_yaml(p):
            DBSession.equipments[key] = meta

    for p in iter_dir(ASSETS_DIR / 'colors'):
        for key, color in read_yaml(p):
            r, g, b = color.split(',')
            DBSession.colors[key] = QColor(int(r), int(g), int(b))

    # 加载资源
    for key, meta in read_yaml('resources/material.yaml'):
        DBSession.materials[key] = Resource(key, **meta)

    # 加载任务
    for p in iter_dir(ASSETS_DIR / 'quest'):
        for key, meta in read_yaml(p):
            meta['category'] = getattr(QuestCategory, meta['category'])
            cls = getattr(module_quest, 'Quest' + meta['type'])
            quest = cls(key, **meta)
            DBSession.quests[key] = quest
            if quest.requirement is None:
                quest.status = QuestStatus.Accessible

    # 加载图谱
    tree_set = list(read_yaml('tree_map/tech.yaml'))
    for key, meta in (i for i in tree_set if 'parents' not in i[1]):
        DBSession.tree[key] = TreeNode(key, meta['left'], meta['top'])
    for key, meta in (i for i in tree_set if 'parents' in i[1]):
        for parent_name in meta['parents']:
            parent_node: TreeNode = DBSession.tree[parent_name]
            tree_node = TreeNode(key, meta['left'], meta['top'])
            DBSession.tree[key] = tree_node
            tree_node.parents.append(parent_node)
            parent_node.children.append(tree_node)

    # 加载传送口
    for identifier, name in read_yaml('portals.yaml'):
        DBSession.portals[identifier] = Portal(name, identifier)
    # 加载传送链接
    for link_id, left_and_right in read_yaml('links.yaml'):
        left, right = left_and_right.split(',')
        left_portal: Portal = DBSession.portals[left]
        right_portal: Portal = DBSession.portals[right]
        link = LinkPortal(left_portal, right_portal)
        left_portal.link = link
        right_portal.link = link
    # 加载地图
    for map_file in iter_dir(ASSETS_DIR / 'lattice_map'):
        map_name = map_file.name.split('.')[0]
        meta: dict = read_yaml(map_file, _iter=False)
        lattice_map = LatticeMap(meta['name'])
        for portal_meta in meta.get('portals', []):
            portal: Portal = DBSession.portals[portal_meta['key']]
            portal.map_name = map_name
            portal.pos = Pos(*eval(portal_meta['pos']))
            lattice_map.set_build(portal.pos, portal)
        for npc_meta in meta.get('npc', []):
            _npc: Npc = DBSession.npc_map[npc_meta['key']]
            pos = Pos(*eval(npc_meta['pos']))
            lattice_map.set_npc(pos, _npc)
        DBSession.lattice_map[map_name] = lattice_map
